package com.xiaoyao.myframeapp.utils.db;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.xiaoyao.myframeapp.FrameApplication;
import com.xiaoyao.myframeapp.utils.L;
import com.xiaoyao.myframeapp.utils.Time;
import com.xiaoyao.myframeapp.utils.getApp;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.ex.DbException;

/**
 * @Author xiaoyao
 * @Email xiao80206040@qq.com
 * @Explain TODO 根据用户表 进行对象json保存 自定义缓存数据库
 * @Created by Administrator on 2017/8/30 0030 11:47
 */
//TODO json 》》类《《 json

@Table(name = "DbDatafromJson")
public class DbDatafromJson {


    /**
     * 返回一个保存对象
     *
     * @param name
     * @param db
     * @return
     */
    public static DbDatafromJson getDataString(@NonNull String name, @NonNull DbManager db) {
        try {
            DbDatafromJson dbDatafromJson = db.selector(DbDatafromJson.class).where("nameX", "=", name).findFirst();
            if (dbDatafromJson == null) {
                L.e("1");
                return null;
            }
            String VersionCode = getApp.getAppVersionCode(FrameApplication.getmContext());

            if (!TextUtils.isEmpty(VersionCode) && dbDatafromJson.getVersionCode() > 0) {
                if (Integer.valueOf(VersionCode) != dbDatafromJson.getVersionCode()) {
                    L.e("3");
                    return null;
                }
            } else {
                L.e("2");
                return null;
            }
            return dbDatafromJson;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setDataString(@NonNull String name, @NonNull Object t, @NonNull DbManager db) {
        DbDatafromJson dbDatafromJson;
        try {
            dbDatafromJson = db.selector(DbDatafromJson.class).where("nameX", "=", name).findFirst();
            if (dbDatafromJson == null) {
                dbDatafromJson = new DbDatafromJson();
                dbDatafromJson.setName(name);
                dbDatafromJson.setData(FrameApplication.toJson(t));
                dbDatafromJson.setTime(Time.getsjhm());

                dbDatafromJson.setVersionCode(returnVersionCode());

                db.save(dbDatafromJson);
            } else {
                dbDatafromJson.setData(FrameApplication.toJson(t));
                dbDatafromJson.setTime(Time.getsjhm());

                dbDatafromJson.setVersionCode(returnVersionCode());

                db.update(dbDatafromJson);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    private static int returnVersionCode() {
        String VersionCode = getApp.getAppVersionCode(FrameApplication.getmContext());
        if (TextUtils.isEmpty(VersionCode)) {
            return 0;
        } else {
            return Integer.valueOf(VersionCode);
        }
    }


    @Column(name = "id", isId = true, autoGen = true, property = "NOT NULL")
    private int id;

    @Column(name = "nameX")
    private String name;

    @Column(name = "data")
    private String data;

    @Column(name = "time")
    private String time;

    @Column(name = "versionCode")
    private int versionCode;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }
}
